package com.cloud.drore.eboss.operation.controller;


import com.cloud.drore.eboos.common.base.PagePo;
import com.cloud.drore.eboos.common.base.ResultMessage;

import com.cloud.drore.eboos.common.entity.dealer.MerchantVo;
import com.cloud.drore.eboos.exception.BusinessException;
import com.cloud.drore.eboss.login.user.Anon;
import com.cloud.drore.eboss.operation.entity.Merchant;
import com.cloud.drore.eboss.operation.entity.MerchantManage;
import com.cloud.drore.eboss.operation.entity.MerchantMulti;
import com.cloud.drore.eboss.operation.entity.MerchantRedisAccount;
import com.cloud.drore.eboss.operation.entity.po.MerchantPo;
import com.cloud.drore.eboss.operation.entity.vo.DictionaryMerchantVo;
import com.cloud.drore.eboss.operation.mapper.MerchantMultiMapper;
import com.cloud.drore.eboss.operation.service.IBaseDataFeign;
import com.cloud.drore.eboss.operation.service.impl.MerchantManageServiceImpl;
import com.cloud.drore.eboss.operation.service.impl.MerchantMultiServiceImpl;
import com.cloud.drore.eboss.operation.service.impl.MerchantServiceImpl;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.List;


/**
 * 浙江卓锐科技股份有限公司 版权所有  Copyright 2018<br>
 *
 * @Description: 商户接口<br>
 * @Project: eboos <br>
 * @CreateDate: Created in 2018/1/6 0006 下午 12:50 <br>
 * @Author: <a href="410508961@qq.com">夏凯</a>
 */
@RestController
@RequestMapping("/merchant")
@Api(value = "/商户接口", description = "商户管理接口-夏凯")
public class MerchantController {
    @Resource
    MerchantServiceImpl merchantServiceImpl;
    @Resource
    MerchantManageServiceImpl merchantManageServiceImpl;
    @Autowired
    IBaseDataFeign iBaseDataFeign;
    @Autowired
    MerchantMultiServiceImpl merchantMultiServiceImpl;
    @Autowired
    MerchantMultiMapper merchantMultiMapper;


    @ApiOperation(value = "新增商户信息多选框字段1：具备，0：不具备，多选框信息调用init可拿到")
    @PostMapping(value = "/save")
    public ResultMessage save(@RequestBody @Valid Merchant merchant) {
        //查询字典，做商户更新的准备
        merchant = merchantServiceImpl.selectDictory(merchant);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setId(merchant.getId() + "");
        return resultMessage;
    }


    @ApiOperation(value = "更新商户基本信息:只能上传用户的基本信息")
    @PutMapping(value = "/update")
    public ResultMessage update(@RequestBody @Validated({Merchant.SAVE.class}) Merchant merchant) throws BusinessException {
        //获得商户状态判断商户可修改的字段
        Merchant merchantStateFlag = (Merchant) merchantServiceImpl.selectByPrimaryKey(merchant.getId());
        if (null == merchantStateFlag || "".equals(merchantStateFlag)) {
            throw new BusinessException("该商户不存在！");
        }
        if (merchantServiceImpl.update(merchant) != 1) {
            throw new BusinessException("商户修改信息失败！");
        }
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setId(merchant.getId() + "");
        return resultMessage;
    }

    @ApiOperation(value = "商户信息作废")
    @GetMapping(value = "/delete/{id}")
    public ResultMessage delete(@PathVariable("id") Long id) throws BusinessException {
        ResultMessage resultMessage = new ResultMessage();
        if (merchantServiceImpl.delete(id) == 1) {
            resultMessage.setId(id + "");
            return resultMessage;
        }
        throw new BusinessException("删除商户信息出现异常！");
    }

    @ApiOperation(value = "恢复商户信息")
    @GetMapping(value = "/back/{id}")
    public ResultMessage back(@PathVariable("id") Long id) throws BusinessException {
        ResultMessage resultMessage = new ResultMessage();
        if (merchantServiceImpl.back(id) == 1) {
            resultMessage.setId(id + "");
            return resultMessage;
        }
        throw new BusinessException("恢复商户信息出现异常！");
    }


    @ApiOperation(value = "通过id获取商户信息")
    @GetMapping(value = "/{id}")
    public ResultMessage<Merchant> get(@PathVariable("id") Long id) throws BusinessException {
        Merchant merchant = (Merchant) merchantServiceImpl.selectByPrimaryKey(id);
        if (null == merchant || "".equals(merchant)) {
            throw new BusinessException("该商户不存在！");
        }
        merchant.setGoodsSaleType(merchantServiceImpl.queryMerchantGoodsSaleType(id));
        return new ResultMessage(merchant);
    }

    @Anon
    @ApiOperation(value = "通过id获取商户信息（分销服务远程调用）")
    @GetMapping(value = "selectMerchantFordealer/{id}")
    public ResultMessage<Merchant> selectMerchantFordealer(@PathVariable("id") Long id) throws BusinessException {
        Merchant merchant = (Merchant) merchantServiceImpl.selectByPrimaryKey(id);
        if (null == merchant || "".equals(merchant)) {
            throw new BusinessException("该商户不存在！");
        }
        merchant.setGoodsSaleType(merchantServiceImpl.queryMerchantGoodsSaleType(id));
        return new ResultMessage(merchant);
    }


    @ApiOperation(value = "通过id获取商户信息用于单个查询显示商户信息")
    @GetMapping(value = "/getMerchant/{id}")
    public ResultMessage<Merchant> getMerchant(@PathVariable("id") Long id) {

        return new ResultMessage(merchantServiceImpl.getMerchant(id));
    }


    @ApiOperation(value = "设置管理员")
    @PutMapping(value = "/saveMerchantManager")
    public ResultMessage get(@RequestBody @Valid MerchantManage merchantManage) throws BusinessException {
        if (merchantManageServiceImpl.addMerchantManage(merchantManage) != 1) {
            throw new BusinessException("管理员设置失败！");
        }
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setId(merchantManage.getId() + "");
        return resultMessage;
    }


    @ApiOperation(value = "修改管理员基本信息")
    @PutMapping(value = "/updateMerchantManager")
    public ResultMessage updateMerchantManager(@RequestBody @Valid MerchantManage merchantManage) throws BusinessException {
        merchantManageServiceImpl.updateMerchantManage(merchantManage);

        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setId(merchantManage.getId() + "");
        return resultMessage;
    }

    @ApiOperation(value = "管理员重置密码")
    @GetMapping(value = "/resetPassword/{id}")
    public ResultMessage resetPassword(@PathVariable("id") Long id) throws BusinessException {
        merchantManageServiceImpl.resetManagePassword(id);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setId(id + "");
        return resultMessage;
    }

    @ApiOperation(value = "查看管理员信息")
    @GetMapping(value = "/selectManage/{id}")
    public ResultMessage selectManage(@PathVariable("id") Long id) {
        return merchantManageServiceImpl.findMerchant(id);
    }


    @ApiOperation(value = "初始化商户列表信息")
    @GetMapping(value = "init")
    public ResultMessage<DictionaryMerchantVo> init() {
        return merchantServiceImpl.findByName();
    }

    @ApiOperation(value = "查询商户列表信息")
    @PostMapping(value = "find")
    public ResultMessage find(@RequestBody PagePo<MerchantPo> pagePo) {
        return merchantServiceImpl.findByData(pagePo);
    }

    @Anon
    @ApiOperation(value = "获取供应商列表")
    @GetMapping(value = "getAllSupplierList")
    public ResultMessage<List<Merchant>> getAllSupplierList() {
        return ResultMessage.success(merchantServiceImpl.selectMerchantList());
    }

    @ApiOperation(value = "获取供应商列表键值对字典类型用于资金结算初始化供应商列表")
    @GetMapping(value = "getSupplierListDictionary")
    public ResultMessage<MerchantVo> getSupplierListDictionary() {
        return ResultMessage.success(merchantServiceImpl.getSupplierListDictionary());
    }

    @ApiOperation(value = "获取分销商列表键值对字典类型用于资金结算初始化分销商列表")
    @GetMapping(value = "getDealerListDictionary")
    public ResultMessage<MerchantVo> getDealerListDictionary() {
        return ResultMessage.success(merchantServiceImpl.getDealerListDictionary());
    }

    @Anon
    @ApiOperation(value = "获取分销商列表")
    @GetMapping(value = "getAllDistributorList")
    public ResultMessage<List<Merchant>> getAllDistributorList() {
        return ResultMessage.success(merchantServiceImpl.selectMerchantListDistributor());
    }

    @ApiOperation(value = "根据商户名称查询对应的现金池")
    @PostMapping(value = "selectMerchantCool")
    public ResultMessage<DictionaryMerchantVo> selectMerchantCool(@RequestBody @Valid MerchantRedisAccount merchantRedisAccount) {
        BigDecimal bigDecimal = merchantServiceImpl.queryMerchantCool(merchantRedisAccount);
        return ResultMessage.success(bigDecimal);
    }
}
